﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace ColorSelectorMVC.Helpers
{
    /// <summary>
    /// Helper methods to extend existing html helpers
    /// </summary>
    public static class Helpers
    {
        /// <summary>
        /// Creates a menu item with currently selected tab highlighted
        /// </summary>
        /// <param name="htmlHelper">Html helper class extending from</param>
        /// <param name="text">The text to display in the link</param>
        /// <param name="action">The action of the route</param>
        /// <param name="controller">The controller of the route</param>
        /// <returns></returns>
        public static MvcHtmlString MenuItem(
            this HtmlHelper htmlHelper,
            string text,
            string action,
            string controller)
        {
            var menu = new TagBuilder("li");

            //Verify that the current action and controller were specified by the call and if so mark menu item as active
            var currentAction = (string)htmlHelper.ViewContext.RouteData.Values["action"];
            var currentController = (string)htmlHelper.ViewContext.RouteData.Values["controller"];

            if (string.Equals(currentAction, action, StringComparison.CurrentCultureIgnoreCase) &&
                string.Equals(currentController, controller, StringComparison.CurrentCultureIgnoreCase))
            {
                menu.AddCssClass("active");
            }
            menu.InnerHtml = htmlHelper.ActionLink(text, action, controller).ToHtmlString();
            return MvcHtmlString.Create(menu.ToString());
        }
    }
}